<?php
namespace api\modules\v1\controllers;

use Yii;
use api\common\controllers\AController;
use common\records\Record;
use common\records\Order;
use common\records\Toy;
use common\records\ToyMachine;
use common\records\UserAchievement;
use common\records\Achievement;
use yii\data\ActiveDataProvider;

class ResultController extends AController
{
    public $modelClass = 'common\records\Record';
    /**
     * @api {get} /v1/results 结果查询
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup records
     *
     * @apiParam {String} uid 用户id
     *
     */
    public function actionIndex()
    {
        if(!array_key_exists("uid",$_GET) || !array_key_exists("order_id",$_GET))
        {
            $result = array();
            $detail = array();
            
            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $uid = trim($_GET['uid']);
        $order_id = intval(trim($_GET['order_id']));
        $record = Record::find()->where(['uid'=>$uid, 'order_id'=>$order_id])->one();

        if($record == null)
        {
            $result = array();
            $detail = array();
            
            $detail['code'] = 201;
            $detail['reason'] = "not finish";
            $result['result'] = $detail;
            return $result;
        }
        if($record->is_read == 1)
        {
            $user_achievements = UserAchievement::find()->where(['uid'=>$uid,'order_id'=>$order_id])->all();
            $get_aids = array();
            foreach($user_achievements AS $item)
            {
                $get_aids[] = $item->aid; 
            }
            $achievements = Achievement::find()->where(['in','id',$get_aids])->all();
            $get_achievements = array();
            foreach($achievements AS $achievement)
            {
                $new_achieve = array();
                $new_achieve['id'] = $achievement->id;
                $new_achieve['title'] = $achievement->title;
                $new_achieve['description'] = $achievement->description;
                $new_achieve['photo'] = $achievement->photo;
                $get_achievements[] = $new_achieve;
            }
            $result = array();
            $detail = array();

            $detail['code'] = 200;
            $detail['reason'] = "finish";
            $detail['status'] = $record->status;

            $toy = Toy::find()->where(['id'=> $record->toy_id])->one();
            if($toy != null)
            {
                $detail['toy_name'] = $toy->name;
                $detail['toy_photo'] = $toy->photo;
            }else{
                $detail['toy_name'] = "";
                $detail['toy_photo'] = "";
            }
            $result['result'] = $detail;
            $achievement = array();
            $achievement['achievement']= $get_achievements;

            if(count($get_aids) > 0)
            {
                return array_merge($result, $achievement);
            }
            return $result;
        }

        $try_records = Record::find()->where(['uid'=>$uid,'type'=>2])->all();
        $try_times = count($try_records);
        $success_times = 0;
        foreach($try_records AS $item)
        {
            if($item->status == 1)
                $success_times = $success_times+1;
        }
        $user_get_achievements = UserAchievement::find()->where(['uid'=>$uid])->all();

        $get_aids = array();
        foreach($user_get_achievements AS $achieve)
        {
            $get_aids[]=$achieve->aid;
        }
        $achievements = Achievement::find()->where([])->all();
        $is_new_achievement = false;
        $new_achievements = array();
        foreach($achievements AS $achievement)
        {
            if(in_array($achievement->id, $get_aids))
                continue;
            if($try_times >= $achievement->rule_try && $success_times >= $achievement->rule_success)
            {
                $user_achievement = new UserAchievement();
                $user_achievement->uid = $uid;
                $user_achievement->aid = $achievement->id;
                $user_achievement->timestamp =date('Y-m-d H:i:s',time()); 
                $user_achievement->order_id=$order_id; 
                $user_achievement->save();

                $new_achieve = array();
                $new_achieve['id'] = $achievement->id;
                $new_achieve['title'] = $achievement->title;
                $new_achieve['description'] = $achievement->description;
                $new_achieve['photo'] = $achievement->photo;
                $new_achievements[] = $new_achieve;
                $is_new_achievement = true;
            }
        }
        $result = array();
        $detail = array();

        $detail['code'] = 200;
        $detail['reason'] = "finish";
        $detail['status'] = $record->status;

        $toy = Toy::find()->where(['id'=> $record->toy_id])->one();
        if($toy != null)
        {
            $detail['toy_name'] = $toy->name;
            $detail['toy_photo'] = $toy->photo;
        }else{
            $detail['toy_name'] = "";
            $detail['toy_photo'] = "";
        }
        $result['result'] = $detail;
        $achievement = array();
        $achievement['achievement']= $new_achievements;
        if($is_new_achievement)
        {
            return array_merge($result, $achievement);
        }
        return $result;
    }

    /*
     @param order_id 订单id
     @param uid      用户ID
     @param status   抓娃娃结果
     */
    public function actionCreate()
    {
        if(!array_key_exists("order_id",$_POST) || !array_key_exists("uid",$_POST)|| !array_key_exists("is_read",$_POST) )
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "字段缺失";
            $result['result'] = $detail;
            return $result;

        }
        $order_id = intval(trim($_POST['order_id']));
        $uid = trim($_POST['uid']);
        $is_read = intval(trim($_POST['is_read']));
        $order_info = Order::find()->where(['order_id'=>$order_id, 'uid'=>$uid])->one();
        if($order_info == null)
        {
            $result = array();
            $detail = array();

            $detail['code'] = 401;
            $detail['reason'] = "用户不拥有该订单";
            $result['result'] = $detail;
            return $result;
        }

        $record= Record::find()->where(['uid'=>$uid,'order_id'=>$order_id])->one();
        if($record == null)
        {
            $result = array();
            $detail = array();
            
            $detail['code'] = 404;
            $detail['reason'] = "订单未完成";
            $result['result'] = $detail;
            return $result;
        }
        $record->is_read = $is_read;
        $record->save(false);
        $result = array();
        $detail = array();

        $detail['code'] = 200;
        $detail['reason'] = "已读结果同步成功";
        $result['result'] = $detail;
        return $result;
    }
}
